import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class CustomerManageService {
  serviceUrl = {
    emailList: '/news/list', // 站内信列表
    sendEmail: '/news/save', // 添加站内信
    editEmail: '/news/update', // 修改站内信
    emailDetail: '/news/details', // 站内信详情
    withdraw: '/news/withdraw', // 站内信撤回
    emailCheck: '/news/audit', // 站内信审核
    calcUser: '/news/getUser', // 计算符合条件的用户数量
    dict: '/dict/detail', // 字典
    fileUpload: '/common/upload/file', // 文件上传
    advanceStopUrl: '/news/updateState', // 提前终止
  };
  belongOptions = [];

  constructor(private httpClient: HttpClient) {
    this.getDict();
  }

  // 获取字典
  async getDict() {
    await this.httpGet(this.serviceUrl.dict, {
      pageNum: 1,
      pageSize: 999,
      parentKey: 'business'
    }).then((res: any) => {
      if (res.code === 0) {
        this.belongOptions = res.data.list.records;
      }
    });
  }

  getBusinessName(id: any) {
    if (this.belongOptions.length) {
      const list = this.belongOptions.filter((item: any) => item.content == id);
      if (list.length) {
        return list[0]['name'];
      } else {
        return '未知';
      }
    } else {
      return '字典未加载';
    }
  }

  computedColor(status: number) {
    let color = '';
    let name = '';
    const btn = new Set();
    switch (status) {
      case 0:
        color = '#999999';
        name = '草稿';
        btn.add('submitCheck');
        btn.add('edit');
        break;
      case 1:
        color = '#FF6600';
        name = '待自动发送';
        btn.add('cancel');
        btn.add('view');
        break;
      case 2:
        color = '#008000';
        name = '已发送';
        btn.add('withdraw');
        btn.add('view');
        break;
      case 3:
        color = '#999999';
        name = '已撤回';
        btn.add('view');
        break;
      case 4:
        color = '#FF9900';
        name = '已取消';
        btn.add('view');
        break;
      case 5:
        color = '#FF9900';
        name = '待审核';
        btn.add('check');
        btn.add('view');
        break;
      case 6:
        color = '#FF0000';
        name = '审核不通过';
        btn.add('edit');
        break;
      case 7:
        color = '#FF9900';
        name = '进行中';
        // btn.add('edit');
        break;
      case 8:
        color = '#FF0000';
        name = '已终止';
        // btn.add('edit');
        break;
      case 999:
        color = 'black';
        name = '初始';
        btn.add('edit');
        break;
      default:
        color = 'black';
        break;
    }
    return {color, name, btn};
  }

  formatTime(date: any, type: 'date' | 'dateTime' | 'time') {
    if (!date || (typeof date) === 'string') {
      return date || '';
    }
    let result = '';
    switch (type) {
      case 'date':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0];
        break;
      case 'dateTime':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0] + ' ' + date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
      case 'time':
        // @ts-ignore
        result = date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
    }
    return result;
  }

  httpGet(url: string, params: any) {
    return this.httpClient.get(url, {params}).toPromise();
  }

  httpPost(url: string, body: any) {
    // return this.httpClient.post(url, body).toPromise().then((res: any) => {
    //   return res.code === 0 ? res.data : {current: 0, pages: 0, records: [], searchCount: 0, size: 0, total: 0};
    // });
    return this.httpClient.post(url, body).toPromise();
  }
}
